package com.sky.web;

import com.alibaba.fastjson2.JSON;
import com.sky.api.member.MemberFeignService;
import com.sky.constant.AuthConstant;
import com.sky.dto.member.UmsMemberVO;
import com.sky.entity.GiteeEntity;
import com.sky.entity.GiteeUserInfoEntity;
import com.sky.utils.HttpUtils;
import com.sky.utils.R;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

@Controller
public class GiteeLoginController {

    @Value("${spring.security.oauth2.client.registration.gitee.client-id}")
    private String clientId;
    @Value("${spring.security.oauth2.client.registration.gitee.client-secret}")
    private String clientSecret;
    @Value("${spring.security.oauth2.client.registration.gitee.authorization-grant-type}")
    private String authorizationGrantType;

    @Resource
    private MemberFeignService memberFeignService;

    @GetMapping("/login/oauth2/code/gitee")
    public String giteeLogin(@RequestParam("code") String code, HttpSession session) throws Exception{
        System.out.println("获取到的code：" + code);
        // 使用code换取token
        Map<String, String> bodyMap = new HashMap<>(16);
        bodyMap.put("client_id", clientId);
        bodyMap.put("client_secret", clientSecret);
        bodyMap.put("grant_type", authorizationGrantType);
        bodyMap.put("redirect_uri", "http://auth.skymall.com/login/oauth2/code/gitee");
        bodyMap.put("code", code);
        try {
            String result = HttpUtils.sendFormPost("https://gitee.com/oauth/token", null, bodyMap);
            GiteeEntity giteeEntity = JSON.parseObject(result, GiteeEntity.class);
            System.out.println("获取到的token：" + giteeEntity.getAccess_token());
            // 查询用户的基本信息
            bodyMap = new HashMap<>(16);
            bodyMap.put("access_token", giteeEntity.getAccess_token());
            String userInfoStr = HttpUtils.sendFormGet("https://gitee.com/api/v5/user", null, bodyMap);
            GiteeUserInfoEntity userInfoEntity = JSON.parseObject(userInfoStr, GiteeUserInfoEntity.class);
            giteeEntity.setUserInfoEntity(userInfoEntity);
            // 用户登录
            R<UmsMemberVO> login = memberFeignService.giteeLoginByToken(giteeEntity);
            if (login.getCode() == 0){
                // 跳转到商品页面
                session.setAttribute(AuthConstant.SESSION_USER_INFO, login.getData());
                return "redirect:http://skymall.com";
            } else {
                return "redirect:http://auth.skymall.com";
            }
        } catch (Exception e) {
            return "redirect:http://auth.skymall.com";
        }
    }
}
